﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace HRDexCapstoneProject.Functions.Forms
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        string SessionID()
        {
            return Session["EmployeeID"].ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlCommand cmd22 = new SqlCommand("SELECT EmployeeID, LastName + ', ' + FirstName + ' ' + MiddleName AS Name, DeptID, catPositionsID, CollegeID FROM formEmployees WHERE (EmployeeID ="+SessionID()+")", conn);
            SqlDataAdapter da22 = new SqlDataAdapter(cmd22);
            DataTable dt22 = new DataTable("dt22");

            da22.Fill(dt22);

            txtEmpNo.Text = dt22.Rows[0].ItemArray[0].ToString();
            txtEmpName.Text = dt22.Rows[0].ItemArray[1].ToString();
            ddlPositions.SelectedValue = dt22.Rows[0].ItemArray[3].ToString();
            ddlOffice.SelectedValue = dt22.Rows[0].ItemArray[4].ToString();

            Selected();
        }
        SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True");

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();

            conn.Open();

            SqlCommand cmd = new SqlCommand("Insert Into formFlexi (EmpID, CollegeID, PositionID, originalDate, originalTime, ActivityDesc, FlexiDate, FlexiTime, PubDate) Values (@empID, @collegeID, @posID, @oDate, @oTime, @actDesc, @fDate, @fTime, @pubDate)", conn);

            cmd.Parameters.Add("@empID", SqlDbType.Int).Value = txtEmpNo.Text;
            cmd.Parameters.Add("@collegeID", SqlDbType.Int).Value = ddlOffice.SelectedValue;
            cmd.Parameters.Add("@posID", SqlDbType.Int).Value = ddlPositions.SelectedValue;
            cmd.Parameters.Add("@oDate", SqlDbType.VarChar).Value = DPOriginal.SelectedDates;
            cmd.Parameters.Add("@oTime", SqlDbType.VarChar).Value = ddlOhr.Text + ":" + ddlOmin.Text;
            cmd.Parameters.Add("@actDesc", SqlDbType.VarChar).Value = txtActivityReason.Text;
            cmd.Parameters.Add("@fDate", SqlDbType.VarChar).Value = DPFlexi.SelectedDates;
            cmd.Parameters.Add("@fTime", SqlDbType.VarChar).Value = ddlFlexiHour.Text + ":" + ddlFlexiMin.Text;
            cmd.Parameters.Add("@pubDate", SqlDbType.DateTime).Value = DateTime.Now;
            cmd.ExecuteNonQuery();

            conn.Close();
        }
        public void Selected()
        {
            string FlexiID = Request.QueryString["FlexiID"];
            if (FlexiID != null)
            {
                DPFlexi.Visible = false;
                DPOriginal.Visible = false;
                ddlOhr.Visible = false;
                ddlOmin.Visible = false;
                ddlFlexiHour.Visible = false;
                ddlFlexiMin.Visible = false;

                SqlCommand cmd2 = new SqlCommand("SELECT formFlexi.FlexiID, formEmployees.LastName + ', ' + formEmployees.FirstName + ' ' + formEmployees.MiddleName AS Name, " +
                    "formEmployees.EmployeeID, formFlexi.PositionID, formFlexi.CollegeID, " +
                    "formFlexi.FlexiDate, formFlexi.FlexiTime, formFlexi.ActivityDesc, formFlexi.OriginalDate, " +
                    "formFlexi.OriginalTime, formFlexi.pubDate FROM formFlexi INNER JOIN formEmployees ON " +
                    "formFlexi.EmpID = formEmployees.EmployeeID AND formFlexi.EmpID = formEmployees.EmployeeID " +
                    "WHERE (FlexiID = " + FlexiID + ")", conn);
                SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
                DataTable dt2 = new DataTable("dt2");

                da2.Fill(dt2);
                txtEmpNo.Text = dt2.Rows[0].ItemArray[2].ToString();
                txtEmpName.Text = dt2.Rows[0].ItemArray[1].ToString();
                ddlPositions.SelectedValue = dt2.Rows[0].ItemArray[3].ToString();
                ddlOffice.SelectedValue = dt2.Rows[0].ItemArray[4].ToString();
                txtfDate.Text = dt2.Rows[0].ItemArray[5].ToString();
                txtfTime.Text = dt2.Rows[0].ItemArray[6].ToString();
                txtActivityReason.Text = dt2.Rows[0].ItemArray[7].ToString();
                txtODate.Text = dt2.Rows[0].ItemArray[8].ToString();
                txtOTime.Text = dt2.Rows[0].ItemArray[9].ToString();
                lblDatePub.Text = dt2.Rows[0].ItemArray[10].ToString();


                //Approval Enable Dept Head
                if (dt2.Rows[0].ItemArray[12].ToString() != "0" && dt2.Rows[0].ItemArray[12].ToString() != "1")
                {
                    ddlApproval2.Enabled = false;
                    ddlApproval1.Enabled = true;
                    ddlApproval3.Enabled = false;
                }
                else if (dt2.Rows[0].ItemArray[13].ToString() != "0" && dt2.Rows[0].ItemArray[13].ToString() != "1")
                {
                    ddlApproval2.Enabled = true;
                    ddlApproval1.Enabled = false;
                    ddlApproval3.Enabled = false;
                }
                else if (dt2.Rows[0].ItemArray[14].ToString() != "0" && dt2.Rows[0].ItemArray[14].ToString() != "1")
                {
                    ddlApproval2.Enabled = false;
                    ddlApproval1.Enabled = false;
                    ddlApproval3.Enabled = true;
                }
                else
                {
                    ddlApproval2.Enabled = false;
                    ddlApproval1.Enabled = false;
                    ddlApproval3.Enabled = false;
                }

            }
        }
        public void Edit()
        {
            string LeaveID = Request.QueryString["LeaveID"];
            SqlCommand cmd = new SqlCommand("UPDATE formLeave SET  ApprovalIDHead =" + ddlApproval1.Text + " WHERE(LeaveID = " + LeaveID + ")", conn);
            SqlCommand cmd22 = new SqlCommand("UPDATE formLeave SET  ApprovalIDDean =" + ddlApproval2.Text + " WHERE(LeaveID = " + LeaveID + ")", conn);
            SqlCommand cmd33 = new SqlCommand("UPDATE formLeave SET  ApprovalIDHR =" + ddlApproval3.Text + " WHERE(LeaveID = " + LeaveID + ")", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            SqlDataAdapter da22 = new SqlDataAdapter(cmd22);
            SqlDataAdapter da33 = new SqlDataAdapter(cmd33);
            conn.Open();
            cmd.Parameters.AddWithValue("ApprovalIDHead", ddlApproval1.Text);
            cmd22.Parameters.AddWithValue("ApprovalIDDean", ddlApproval2.Text);
            cmd33.Parameters.AddWithValue("ApprovalIDHR", ddlApproval3.Text);
            cmd.ExecuteNonQuery();
            conn.Close();

        }

    }
}